from rflysim import RflysimEnvConfig
from rflysim.client import VehicleClient
from rflysim.config import AreaInfo,Position
import time
if __name__ == "__main__":
    # 环境配置
    config = {
        "id": 102,                        # 填入网页中参赛方案对应的方案号
        "config": RflysimEnvConfig(
            "127.0.0.1",              # 填入对应的ip地址
            16001,
            18001
        )
    }

    # 初始化client
    client = VehicleClient(id=config["id"], config=config["config"])
    client.enable_rflysim()

    res = client.get_areas()
    for id in res:
        print(id.id, id.fanm, id.name, id.type, id.sim_type, id.nZoneType, id.nNum, id.nPoints, id.LONLAT, id.nRadius)
        for pkg in id.packages:
            print(pkg.id, pkg.weight, pkg.status, pkg.area_id, pkg.vehicle_id)
    print("----------获取车辆载重----------")
    res = client.get_vehicle_capacity()
    for info in res:
        print(info.id, info.cap)

    print("----------获取区域信息接口----------")
    res = client.get_areas()
    for id in res:
        print(id.id, id.fanm, id.name, id.type, id.sim_type, id.nZoneType, id.nNum, id.nPoints, id.LONLAT)
        for pkg in id.packages:
            print(pkg.id, pkg.weight, pkg.status, pkg.area_id, pkg.vehicle_id)

    print("----------获取货物信息----------")
    all_goods = client.get_goods_info()
    for pkg in all_goods:
        print(pkg.id, pkg.weight, pkg.status, pkg.area_id, pkg.vehicle_id)

    package_id = 66 # 货物id
    vehicle_id = 10094 # 载具id
    ared_id = 51 # 卸货区域id
    print("----------前往非取货区----------")
    res = client.set_tactical_maneuver(vehicle_id=vehicle_id,position=[Position(107.01972611746532 , 23.666265802992996, 0)],speed=0.5,ispatrol=False)
    print(res)
    time.sleep(60)

    print("----------尝试取货----------")
    res = client.pick_up_goods(vehicle_id, package_id)
    all_goods = client.get_goods_info()
    for pkg in all_goods:
        if pkg.id == package_id:
            print(pkg.id, pkg.weight, pkg.status, pkg.area_id, pkg.vehicle_id)
            break

    print("----------前往取货区----------")
    res = client.set_tactical_maneuver(vehicle_id=vehicle_id,position=[Position(107.02406688285004 , 23.66675303461041, 0)],speed=90,ispatrol=False)
    print(res)
    time.sleep(60)

    print("----------取货----------")
    res = client.pick_up_goods(vehicle_id, package_id)
    all_goods = client.get_goods_info()
    for pkg in all_goods:
        if pkg.id == package_id:
            print(pkg.id, pkg.weight, pkg.status, pkg.area_id, pkg.vehicle_id)
            break

    print("----------前往非卸货区----------")
    res = client.set_tactical_maneuver(vehicle_id=vehicle_id,position=[Position(107.02249396582715 , 23.665764301130295, 0)],speed=90,ispatrol=False)
    print(res)
    time.sleep(8)

    print("----------尝试卸货----------")
    res = client.delivery_goods(vehicle_id, package_id, ared_id)
    all_goods = client.get_goods_info()
    for pkg in all_goods:
        if pkg.id == package_id:
            print(pkg.id, pkg.weight, pkg.status, pkg.area_id, pkg.vehicle_id)
            break
    
    print("----------前往卸货区----------")
    res = client.set_tactical_maneuver(vehicle_id=vehicle_id,position=[Position(107.02168508124635 , 23.66400158717866, 0)],speed=90,ispatrol=False)
    print(res)
    time.sleep(60)

    print("----------卸货----------")
    res = client.delivery_goods(vehicle_id, package_id, ared_id)
    all_goods = client.get_goods_info()
    for pkg in all_goods:
        if pkg.id == package_id:
            print(pkg.id, pkg.weight, pkg.status, pkg.area_id, pkg.vehicle_id)
            break

